package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.GetMealDetailListBRequest;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.payconfig.MealDetailBVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfMealInfo;
import com.ocom.payconfig.service.XfMealInfoService;
import com.ocom.payconfig.vo.DeleteMealVo;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class XfMealInfoServiceImpl  extends ServiceImpl<IDaoXfMealInfo, EntityXfMealInfo> implements XfMealInfoService {


    @Autowired
    private IDaoXfMealInfo xfMealInfo;

    @Resource
    RedisUtil redisUtil;

    //2.1.获取餐段名称信息(getMealNameInfo)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntityXfMealInfo> GetMealNameInfo(Integer id, Long comId, String mealNo, String mealName) {
        QueryWrapper<EntityXfMealInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        if (StringUtils.isNotNull(id)){
            query.eq("id",id);
        }
        if (StringUtils.isNotEmpty(mealNo)){
            query.eq("meal_no",mealNo);
        }
        if (StringUtils.isNotEmpty(mealName)){
            query.eq("meal_name",mealName);
        }
        return this.list(query);
    }

    // 2.3.添加餐段名称信息(addMealNameInfo)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public void AddMealNameInfo(Long comId, String mealNo, String mealName) {
        EntityXfMealInfo entityXfMealInfo = new EntityXfMealInfo();
        entityXfMealInfo.setComId(comId.intValue());
        entityXfMealInfo.setMealNo(mealNo);
        entityXfMealInfo.setMealName(mealName);
        redisUtil.dels("xf_meal_time#comId:"+comId);
        xfMealInfo.insert(entityXfMealInfo);
    }

    // 2.5.修改餐段名称信息(updateMealNameInfo)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public void UpdateMealNameInfo(Long id, Long comId, String mealNo, String mealName) {
        QueryWrapper<EntityXfMealInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("id",id);
        EntityXfMealInfo entityXfMealInfo = new EntityXfMealInfo();
        entityXfMealInfo.setComId(comId.intValue());
        entityXfMealInfo.setMealNo(mealNo);
        entityXfMealInfo.setMealName(mealName);
        redisUtil.dels("xf_meal_time#comId:"+comId);
        xfMealInfo.update(entityXfMealInfo,query);
    }

    /**
     * 2.7.删除餐段名称信息(deleteMealNameInfo)
     * */
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void DeleteMealNameInfo(Long comId, DeleteMealVo deleteMealVo) {
        QueryWrapper<EntityXfMealInfo> query = Wrappers.query();
        query.eq("com_id",deleteMealVo.getComId());
        if (StringUtils.isNotNull(deleteMealVo.getId())){
            query.eq("id",deleteMealVo.getId());
        }
        redisUtil.dels("xf_meal_time#comId:"+comId);
        query.eq("meal_no",deleteMealVo.getMealNo());
        xfMealInfo.delete(query);
    }

    //获取设备消费汇总报表
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntityXfMealInfo> getMealConsumeReports(Long comId) {
        return xfMealInfo.getMealConsumeReports(comId);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<MealDetailBVo> getMealDetailListB(Long comId,GetMealDetailListBRequest request) {
        QueryWrapper<MealDetailBVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mi.com_id",request.getComId())
                .gt("mt.point_id",0)
                .eq(ObjectUtil.isNotEmpty(request.getMealNo()),"mt.meal_no",request.getMealNo())
                .eq(ObjectUtil.isNotEmpty(request.getPointId()),"mt.point_id",request.getPointId())
                .orderByAsc("mt.point_id","mt.acc_type_id");
        return xfMealInfo.getMealDetailListB(queryWrapper);
    }
}
